import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;

import org.joda.time.DateTime;

class CommandParser{
	ArrayList<String> command;
	String line;
	public CommandParser(String commandString) throws Exception{
		line = commandString;
		command = getList();
	}	
	
	private ArrayList<String> getList() throws Exception{
		
		ArrayList<String> commandList = (ArrayList<String>) Arrays.asList(line.split(" "));
		
		return commandList;
	}
		
	public CommandType getCmdType(){
		String ct = command.get(0);
		if (ct == "add") return CommandType.ADD;
		else if (ct == "del") return CommandType.DELETE;
		else if (ct == "mk") return CommandType.MARK;
		else if (ct == "um") return CommandType.UNMARK;
		else if (ct == "clear") return CommandType.CLEAR;
		else return CommandType.INVALID;
	}
	public String getTaskDescription(){
		String taskTitle = null;
		String[] tokens = null;
		if(command.get(0) == "add"){
			tokens = line.split("by");
			if (tokens.length!= 1){
				return tokens[1];
			}
			else return tokens[0].substring(4);
		}
		return taskTitle;
	}
	
    public Vector<Integer> getTaskNum(){
    	Vector<Integer> index = new Vector<Integer>();
    	if((command.get(0) == "del")||(command.get(0) == "mk")||(command.get(0) == "um")){
    		int cmdLength = command.get(0).length();
    		String numbers = line.substring(cmdLength+1);
    		if(checkInvalid(numbers)){
    			index.add(-1);
    		}// a vector only contains -1 represents invalid
    		else{
    			int numOfTask = command.size() - 1;
    		    if (numOfTask == 1){
    		    	index.add(Integer.parseInt(command.get(1)));
    		    	}
    		    else {
    		    	for(int i = 0 ; i < numOfTask ; i ++ ){
    		    		index.add(Integer.parseInt(command.get(i + 1)));
    		    		}
    		    	}
    		    }
    		}
    		
    	return index;	
    	}
    public boolean checkInvalid(String str){
    	for(int i=0;i<str.length();i++){
    		if((str.charAt(i)<'0')||(str.charAt(i)>'9')){//check if numeric
    			return true;
    		}
    	}
    	return false;
    }
    public DateTime getTaskDeadline(){
    	String time = getTimeString();
    	String dayNtime[] = time.split(" "); //suppose the format is DD/MM/YYYY hr:minute
    	String days = dayNtime[0]; //day is DD/MM/YYYY
    	String minutes = dayNtime[1]; //minute is hr:minute 
    	String[] date = days.split("/");
    	int year = Integer.parseInt(date[0]);
    	int month = Integer.parseInt(date[1]);
    	int day = Integer.parseInt(date[2]);
    	String[] times = minutes.split(":");
    	int hour = Integer.parseInt(times[0]);
    	int minute = Integer.parseInt(times[1]);
    	DateTime dateTime = new DateTime(year, month, day, hour, minute, 0, 0);
    	return dateTime;
    	
    }

	public String getTimeString() {
		String timeString = null;
    	if(command.get(0) == "add"){
    		String[] tokens = line.split("by"); //the format is "add <task> by <time>"
        		if(tokens.length == 1) {//cannot split by "by"
        			timeString = null;
        		}
        		else{
        			timeString = tokens[2];
        		}
        	}
			return timeString;
		}
}
		